<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="商品名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入商品名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品类型" prop="category">
        <el-select v-model="queryParams.category" placeholder="请选择商品类型" clearable>
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:goods:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="goodsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商品名" align="center" prop="name" />
      <el-table-column label="商品类型" align="center" prop="categoryName"/>
      <el-table-column label="库存" align="center" prop="stock" />
      <el-table-column label="兑换积分" align="center" prop="points" />
      <el-table-column label="价格" align="center" prop="price" />
      <el-table-column label="直购价" align="center" prop="directPrice" />
      <el-table-column label="人气指数" align="center" prop="exponent" />
      <el-table-column label="商品描述" align="center" prop="remark" />
      <el-table-column label="评论数" align="center" prop="commentCount" />
      <el-table-column label="1=品牌，2=放心兑，3=七天无理由" align="center" prop="label"/>
      <el-table-column label="封面图" align="center" prop="coverUrl" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.coverUrl" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="已售" align="center" prop="sold" />
      <el-table-column label="已售百分比" align="center" prop="soldPer" />
      <el-table-column label="好评率" align="center" prop="favorableRate" />
      <el-table-column label="0=不是 1=是  限时抢购" align="center" prop="isLimit" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:goods:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:goods:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="商品名" prop="name">
          <el-input v-model="form.name" placeholder="请输入商品名" />
        </el-form-item>
        <el-form-item label="商品类型" prop="category">
          <el-select v-model="form.category" placeholder="请选择商品类型">
            <el-option
              v-for="item in categoryList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库存" prop="stock">
          <el-input v-model="form.stock" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="兑换积分" prop="points">
          <el-input v-model="form.points" placeholder="请输入兑换积分" />
        </el-form-item>
        <el-form-item label="价格" prop="price">
          <el-input v-model="form.price" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="直购价" prop="directPrice">
          <el-input v-model="form.directPrice" placeholder="请输入直购价" />
        </el-form-item>
        <el-form-item label="人气指数" prop="exponent">
          <el-input v-model="form.exponent" placeholder="请输入人气指数" />
        </el-form-item>
        <el-form-item label="商品描述" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入商品描述" />
        </el-form-item>
        <el-form-item label="标签" prop="label">
          <el-input v-model="form.label" placeholder="请输入1=品牌，2=放心兑，3=七天无理由" />
        </el-form-item>
        <el-form-item label="封面图" prop="coverUrl">
          <image-upload v-model="form.coverUrl"/>
        </el-form-item>
        <el-form-item label="已售" prop="sold">
          <el-input v-model="form.sold" placeholder="请输入已售" />
        </el-form-item>
        <el-form-item label="已售%" prop="soldPer">
          <el-input v-model="form.soldPer" placeholder="请输入已售百分比" />
        </el-form-item>
        <el-form-item label="好评率" prop="favorableRate">
          <el-input v-model="form.favorableRate" placeholder="请输入好评率" />
        </el-form-item>
        <el-form-item label="限时抢购" prop="isLimit">
          <el-input v-model="form.isLimit" placeholder="限时抢购(1是0否)" />
        </el-form-item>
        <el-form-item label="评论数" prop="commentCount">
          <el-input v-model="form.commentCount" placeholder="请输入评论数" />
        </el-form-item>
        <el-form-item label="商品评论" prop="commentList">
          <div v-for="(item,index) in commentList" :key="item.id" class="dynamic-input-group">
            <el-input v-model="item.remark" placeholder="请输入评论语" style="width: calc(100% - 50px);" />
            <el-button type="danger" icon="el-icon-minus" @click="removeComment(index)" v-if="commentList.length > 0"></el-button>
          </div>
          <el-button type="primary" icon="el-icon-plus" @click="addComment">添加</el-button>
        </el-form-item>
        <el-form-item label="商品规格" prop="specList">
          <div v-for="(item,index) in specList" :key="item.id" class="dynamic-input-group">
            <el-select v-model="item.specId" placeholder="请选择规格">
              <el-option
                v-for="dict in dictList"
                :key="dict.id"
                :label="dict.name"
                :value="dict.id"
              ></el-option>
            </el-select>
            <el-input v-model="item.typeName" placeholder="请输入规格值" style="width: calc(100% - 50px);" />
            <el-input v-model="item.directPrice" placeholder="请输入直购价" style="width: calc(100% - 50px);" />
            <el-input v-model="item.points" placeholder="请输入积分" style="width: calc(100% - 50px);" />
            <image-upload v-model="item.typePic"/>
            <el-button type="danger" icon="el-icon-minus" @click="removeInput(index)" v-if="specList.length > 0"></el-button>
          </div>
          <el-button type="primary" icon="el-icon-plus" @click="addInput">添加</el-button>
        </el-form-item>
        <el-form-item label="商品详情" prop="details">
          <editor v-model="form.details" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGoods, getGoods, delGoods, addGoods, updateGoods,queryCategoryList,queryDictList } from "@/api/goods/goodsList";

export default {
  name: "Goods",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品列表表格数据
      goodsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        category: null,
        stock: null,
        points: null,
        price: null,
        exponent: null,
        sold: null,
        label: null,
        status: null,
        coverUrl: null,
        directPrice: null,
        soldPer: null,
        favorableRate: null,
        isLimit: null,
        details: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "商品名不能为空", trigger: "blur" }
        ],
        category: [
          { required: true, message: "商品类型不能为空", trigger: "change" }
        ],
        stock: [
          { required: false, message: "库存不能为空", trigger: "blur" }
        ],
        points: [
          { required: false, message: "兑换积分不能为空", trigger: "blur" }
        ],
        exponent: [
          { required: false, message: "人气指数不能为空", trigger: "blur" }
        ],
        sold: [
          { required: false, message: "已售不能为空", trigger: "blur" }
        ],
        remark: [
          { required: false, message: "商品描述不能为空", trigger: "blur" }
        ],
        coverUrl: [
          { required: true, message: "封面图不能为空", trigger: "blur" }
        ],
        directPrice: [
          { required: false, message: "直购价不能为空", trigger: "blur" }
        ],
        soldPer: [
          { required: false, message: "已售百分比不能为空", trigger: "blur" }
        ],
        favorableRate: [
          { required: false, message: "好评率不能为空", trigger: "blur" }
        ],
        isLimit: [
          { required: false, message: "0=不是 1=是  限时抢购不能为空", trigger: "change" }
        ]
      },
      categoryList: [],
      specList: [],
      dictList: [],
      commentList: []
    };
  },
  created() {
    this.getList();
    this.getCategoryList();
  },
  methods: {
      addComment() {
        this.commentList.push({

        });
      },
      removeComment(index) {
        this.commentList.splice(index, 1);
      },
      addInput() {
        this.specList.push({
          specId: '',
          typeName: '',
          typePic: ''
        });
      },

      // 删除某个输入框
      removeInput(index) {
        this.specList.splice(index, 1);
      },
    //查询分类列表
    getCategoryList() {
      this.loading = true;
      queryCategoryList().then(response => {
        this.categoryList = response.data;
      });
    },
    /** 查询商品列表列表 */
    getList() {
      this.loading = true;
      listGoods(this.queryParams).then(response => {
        this.goodsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        category: null,
        stock: null,
        points: null,
        price: null,
        exponent: null,
        sold: null,
        remark: null,
        label: null,
        createTime: null,
        status: null,
        coverUrl: null,
        directPrice: null,
        soldPer: null,
        favorableRate: null,
        isLimit: null
      };
      this.resetForm("form");
      this.specList = [];
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      queryDictList().then(response => {
        // this.dictList = response.data;
        this.open = true;
        this.dictList = response.data;
        this.title = "添加商品列表";
      });

    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getGoods(id).then(response => {
        this.form = response.data;
        this.categoryList = response.data.categoryList;
        this.specList = response.data.specList;
        this.dictList = response.data.dictList;
        this.commentList = response.data.commentList;
        this.open = true;
        this.title = "修改商品列表";
      });

    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateGoods(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addGoods(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品列表编号为"' + ids + '"的数据项？').then(function() {
        return delGoods(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/goods/export', {
        ...this.queryParams
      }, `goods_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
